import { Theme } from "../../models/theme";
import { Banner } from "../../models/banner";
import { Category } from "../../models/category";
import {Activity} from "../../models/activity";
import {SpuPaging} from "../../models/spu-paging";

// pages/home/home.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeA: null,
    themeE: null,
    bannerB: null,
    grid: [],
    activityD: null,
    spuPaging: null,
    loadingType: "loading"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    this.initAllData();
    this.initBottomSpuList();
  },

  async initBottomSpuList() {
    const paging = SpuPaging.getLatestPaging();
    this.data.spuPaging = paging;
    const data = await paging.getMoreData();
    if (!data) {
      return;
    }
    //累加
    wx.lin.renderWaterFlow(data.items);
  },

  async initAllData(){
    const theme = new Theme();
    await theme.getThemes();

    const themeA = theme.getHomeLocationaA();
    const themeE = theme.getHomeLocationaE();
    let themeESpu = [];
    if (themeE.online) {
      const data = await Theme.getHomeLocationESpu();
      if (data) {
        themeESpu = data.spu_list.slice(0, 8);
      }
    }

    const themeF = theme.getHomeLocationaF();

    const bannerB = await Banner.getHomeLocationB();
    const grid = await Category.getHomeLocationC();
    const activityD = await Activity.getHomeLocationD();

    const bannerG = await Banner.getHomeLocationG();

    const themeH = theme.getHomeLocationaH();

    this.setData({
      themeA,
      bannerB,
      themeE,
      themeF,
      themeESpu,
      grid,
      activityD,
      bannerG,
      themeH
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: async function () {
    const data = await this.data.spuPaging.getMoreData();
    if (!data) {
      return;
    }
    wx.lin.renderWaterFlow(data.items);
    //如果没有更多数据，则让页面底部加载切换成end类型（显示"已没有更多数据"）
    if (!data.moreData) {
      this.setData({
        loadingType: "end"
      });
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})